package com.feng.zshop.backend.controller;

import com.feng.zshop.backend.param.CustomerParam;
import com.feng.zshop.backend.service.CustomerService;
import com.feng.zshop.common.constant.PaginationConstant;
import com.feng.zshop.common.exception.ServiceException;
import com.feng.zshop.common.utils.ResponseResult;
import com.feng.zshop.model.Customer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Lee
 * @date 2019/2/24
 */
@Controller
@RequestMapping("/backend/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @GetMapping("/findALl")
    public String findAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
        List<Customer> customers = customerService.findByParam(null);
        PageInfo<Customer> pageInfo = new PageInfo<>(customers);
        model.addAttribute("pageInfo", pageInfo);
        return "customerManager";
    }

    @RequestMapping("/findByParam")
    public String findByParam(CustomerParam customerParam, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, Model model){
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
        List<Customer> customers = customerService.findByParam(customerParam);
        PageInfo<Customer> pageInfo = new PageInfo<>(customers);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("customerParam",customerParam);
        return "customerManager";
    }

    @PostMapping("/getCustomerById")
    @ResponseBody
    public ResponseResult getCustomerById(@RequestParam(value = "id") Integer id) {
        return ResponseResult.successNoMessage(customerService.findById(id));
    }

    @PostMapping("/updateCustomer")
    @ResponseBody
    public ResponseResult updateCustomer(){

        return ResponseResult.successNoData("更新成功");
    }

    @PostMapping("/updateStatus")
    @ResponseBody
    public ResponseResult updateStatus(@RequestParam("id") Integer id){
        try {
            customerService.updateStatus(id);
        } catch (ServiceException e) {
            return ResponseResult.failNoData(e.getMessage());
        }
        return ResponseResult.successNoData("更新状态成功");
    }

}
